<?php


namespace app\api\controller\xilujob;


use app\admin\model\xilujob\JobCategory;
use app\admin\model\xilujob\SinglePage;
use app\common\controller\xilujob\XilujobApi;
use app\common\exception\UploadException;
use app\common\library\Upload;
use app\common\model\xilujob\Area;
use app\common\model\xilujob\Benefit;
use app\common\model\xilujob\CommentTag;
use app\common\model\xilujob\CompanyProperties;
use app\common\model\xilujob\CompanyScale;
use app\common\model\xilujob\Config;
use app\common\model\xilujob\EducationLevel;
use app\common\model\xilujob\IndustryCategory;
use app\common\model\xilujob\InformType;
use app\common\model\xilujob\Navigation;
use app\common\model\xilujob\NewsCategory;
use app\common\model\xilujob\ResumePersonalTag;
use app\common\model\xilujob\Salary;
use app\common\model\xilujob\SkillLevel;
use app\common\model\xilujob\WorkExperience;
use app\common\model\xilujob\WorkState;
use think\App;
use think\Hook;
use think\Config AS ConfigModel;
use think\Lang;

/**
 * 公共方法
 */
class Common extends XilujobApi
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $restrictedActions = ['*'];

    //上传方法
    public function upload()
    {
        // 自定义鉴权判断
        // 强烈建议这里判断$file = $this->request->file('file');的后缀和mimetype

        // 载入语言包，避免出现英文错误提示
        Lang::load(APP_PATH . 'api/lang/zh-cn.php');

        // 获取上传配置
        $uploadConfig = ConfigModel::get("upload");

        // 兼容云存储上传
        if ($uploadConfig['storage'] != 'local') {
            // 这里可以修改允许上传文件的后缀或修改存储的文件路径，例如只允许上传图片
            set_addon_config($uploadConfig['storage'], ['savekey' => '/uploads/{year}{mon}{day}/{filemd5}{.suffix}', 'mimetype' => 'jpg,png,bmp,jpeg,gif'], false);

            // 添加允许上传的行为
            Hook::add('upload_config_checklogin', function () {
                return true;
            });

            request()->param('isApi', true);
            App::invokeMethod(["\\addons\\{$uploadConfig["storage"]}\\controller\\Index", "upload"], ['isApi' => true]);
        } else {
            // 这里可以修改允许上传文件的后缀或修改存储的文件路径，例如只允许上传图片
            //Config::set('upload', array_merge($uploadConfig, ['savekey' => '/uploads/{year}{mon}{day}/{filemd5}{.suffix}', 'mimetype' => 'jpg,png,bmp,jpeg,gif']));

            $attachment = null;
            // 默认普通上传文件
            $file = $this->request->file('file');
            try {
                $upload = new Upload($file);
                $attachment = $upload->upload();
            } catch (UploadException $e) {
                $this->error($e->getMessage());
            }
            return json(['code'=>1, 'msg'=>'上传成功', 'data'=>['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]]);
        }
    }

    /**
     * @ApiTitle (职位举报类型)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.common/job_inform_types)
     */
    public function job_inform_types() {
        $lists = InformType::getAll();
        $this->success('查询成功', $lists);
    }
    /**
     * 获取省市
     */
    public function province_city(){
        $lists = \app\admin\model\xilujob\Area::getProvinceAndCity();
        $this->success('查询成功',$lists);
    }
    /**
     * 获取省市
     */
    public function cities(){
        $areaModel = new Area;
        $lists = $areaModel->field("first")
            ->with(['cities'=>function($q){$q->where('level','2')->where('status','1')->field(['id','name','first']);}])
            ->where('level','2')
            ->where('status','1')
            ->group('first')
            ->order("first",'asc')
            ->select();
        $this->success('查询成功', $lists);
    }
    /**
     * 获得某城市的所有区
     */
    public function districts() {
        $cityId = $this->request->param('city_id');
        $districts = Area::where('level',3)->where('pid', $cityId)
            ->field('id,name')
            ->select();
        $this->success('查询成功',$districts);
    }

    /**
     * 获取省市区
     */
    public function area_tree(){
        $this->success('查询成功',Area::getAreaTree());
    }

    //行业
    public function industry_categories() {
        $categories = IndustryCategory::getAll();
        $this->success('查询成功', $categories);
    }

    public function company_properties() {
        $lists = CompanyProperties::getAll();
        $this->success('查询成功', $lists);
    }

    //规模
    public function company_scales() {
        $types = CompanyScale::getAll();
        $this->success('查询成功', $types);
    }


    /**
     * @ApiTitle (嵌套获取多层职位行业)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.common/job_categories)
     */
    public function job_categories() {
        $tree = JobCategory::getCategoryTree();
        $lists = $tree->getTreeArray(0);
        $this->success('查询成功', $lists);
    }


    /**
     * 返回区， 及旗下所有镇的数据
     */
    public function districts_with() {
        $cityId = $this->request->param('city_id');
        if(!$cityId) {
            $this->error();
        }
        $data = Area::getDistrictsWith($cityId);
        $this->success('查询成功',$data);
    }

    /**
     * @ApiTitle (获取月，天，时薪水)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.common/salary)
     * $ApiParams (name="type",type="int","description":"类型:1=时薪,2=日薪,3=月薪")
     */
    public function salary() {
        $type = $this->request->param('type');
        if($type==1) {
            $lists = Salary::getHourOptions();
        }else if($type==2) {
            $lists = Salary::getDailyOptions();
        }else if($type==3) {
            $lists = Salary::getMonthOptions();

        }else{
            $this->error(__('invalid arguments'));
        }
        $this->success('查询成功', $lists);
    }

    /**
     * @ApiTitle (工作经验)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.common/work_experience)
     */
    public function work_experience() {
        $lists = WorkExperience::getAll();
        $this->success('查询成功', $lists);
    }

    /**
     * @ApiTitle (学历)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.common/education_levels)
     */
    public function education_levels() {
        $lists = EducationLevel::getAll();
        $this->success('查询成功', $lists);
    }

    /**
     * @ApiTitle (可供选择的福利)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.common/benefits)
     */
    public function benefits() {
        $lists = Benefit::getBenefits();
        $this->success('查询成功', $lists);
    }

    /**
     * @ApiTitle (求职状态)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xilujob.common/work_states)
     */
    public function work_states() {
        $lists = WorkState::getAll();
        $this->success('查询成功', $lists);
    }

    /**
     * @ApiTitle (技能熟练度)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xilujob.common/skill_levels)
     */
    public function skill_levels() {
        $lists = SkillLevel::getAll();
        $this->success('查询成功', $lists);
    }

    /**
     * @ApiTitle (标签)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xilujob.common/resume_personal_tags)
     */
    public function resume_personal_tags() {
        $lists = ResumePersonalTag::getAll();
        $this->success('查询成功', $lists);
    }

    /**
     * @ApiTitle (评论标签)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xilujob.common/comment_tags)
     */
    public function comment_tags() {
        $lists = CommentTag::getAll();
        $this->success('查询成功', $lists);
    }


    /**
     * @ApiTitle (新闻分类)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xilujob.common/news_category)
     */
    public function news_category() {
        $lists = NewsCategory::getAll();
        $this->success('查询成功', $lists);
    }

    /**
     * 图片上传
     */
    public function params()
    {
        $name = $this->request->post('name');
        $md5 = $this->request->post('md5');
        $auth = new \addons\alioss\library\Auth();
        $params = $auth->params($name, $md5);
        config('default_return_type','json');//返回json而不是html
        $this->success('', $params);
    }

    public function single_page() {
        $tag = $this->request->param('tag');
        $page = SinglePage::where('short_tag', $tag)->find();
        if($page) {
            $this->success('',['page'=>$page]);
        }
        $this->error('没有相关内容');
    }

    /**
     * 获取客服电话
     */
    public function getphone(){
        $this->success('',['phone'=>config("site.site_contact_phone")]);
    }

    /**
     * 获取对公账号信息
     */
    public function corporate(){
        $config['corporate_company'] = Config::getdatas('corporate_company');
        $config['open_bank'] = Config::getdatas('open_bank');
        $config['corporate_account'] = Config::getdatas('corporate_account');

        $this->success('',$config);
    }

    /**
     * 金刚区
     */
    public function navigation(){
        $group = $this->request->param('group');
        $this->success('',Navigation::getNavigation($group));
    }

}